﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SimpleJson;
using com.mogu.util;
using com.liyu.pluginframe.util;
using System.Threading;

namespace MoguDemo
{
    public partial class Form1 : Form
    {

        private static String appcode = "com.mogu.game.tetris";
        private static String appcode_dt_id = "com.mogu.game.tetris";
        private static String roomid = "10199ae8-9ec2-4c70-b75c-ac81f2039252";
        private static String token = "gamedeveloptoken";
        private static String username = "w1234";

        public Form1()
        {
            InitializeComponent();
        }
        public static void init_user_token()
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://openfire.mmggoomm.com/mogu/get_user_token?username=" + username + "&appcode=" + appcode);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            init_user_token();
            JsonObject j = new JsonObject();
            j.put("username", username);
            j.put("nickname", "昵称");
            j.put("jid", username + "@openfire.mmggoomm.com");
            j.put("debug", true);
            j.put("version", 2);
            j.put("spaceid", roomid);
            j.put("roomid", roomid);
            j.put("token", token);
            j.put("appcode_dt_id", appcode_dt_id);
            j.put("appcode", appcode);
            j.put("author", username);
            JsonObject ju = null;
            JsonObject jn = null;
            List<JsonObject> ja = new List<JsonObject>();
            List<JsonObject> jna = new List<JsonObject>();
            ju = new JsonObject();
            jn = new JsonObject();
            ju.put("username", username);
            ju.put("head", 1);

            jn.put("username", username);
            jn.put("nickname", "昵称");
            ja.Add(ju);
            jna.Add(jn);
            j.put("userlist", ja);
            j.put("nicklist", jna);
            // j.put("game_host", "192.168.1.18");
            j.put("game_host", "openfire.mmggoomm.com");
            j.put("game_point", 3001);
            j.put("token", token);

            IGameSync iii = (IGameSync)new GameSync();
            MainDataTool.setIGameSync(iii);

            //本函数的接口参数是字符串，在android中是Activity，需要注意。
            MainDataTool.getUserInfoJSON(j.ToString());
            Thread t1 = new Thread(new ThreadStart(TestMethod));

        }
        public static void TestMethod()
        {
            UserInfo userInfo = MainDataTool.getUserInfo();
            while (true)
            {
                Thread.Sleep(1000);

                MainDataTool.isMaster();

                MainDataTool.uploadGamePoint("10");

                JsonObject json = new JsonObject();
                json["d"] = 123;
                MainDataTool.pushGameDataToAllUser(json);
            }
        }
    }

}
